import { defineStore } from "pinia";
import { columnFactory, fieldFactory } from "@/views/TableGenerator/util";

export const useTableStore = defineStore("table", {
  state: () => {
    return {
      templateFile:
        localStorage.getItem("table-templateFile") || "Antd-ListPage",
      globalConfig: {},
      currentField: null,
      tableProps: {
        // radio | checkbox
        rowSelectionType: undefined,
        scroll: {
          x: undefined,
          y: undefined,
        },
      },
      fieldList: [
        columnFactory("姓名"),
        columnFactory("性别"),
        columnFactory("抚养人"),
        columnFactory("部门"),
        columnFactory("生活费"),
        columnFactory("操作"),
      ],
    };
  },
  getters: {
    queryList() {
      return this.fieldList
        .filter((field) => field.queryConfig.isQuery)
        .map((field) => field.queryConfig);
    },
    uiType: (state) => {
      const templateStr = state.templateFile;
      return templateStr.slice(0, templateStr.lastIndexOf("-"));
    },
  },
  actions: {
    setCurrentField(field) {
      this.currentField = field;
    },
    setTemplateFile(templateFile) {
      localStorage.setItem("table-templateFile", templateFile);
      this.templateFile = templateFile;
    },
    handleAxureData(data) {
      const { type, config, list } = data;
      if (type === "table") {
        this.fieldList = list.map((data) => {
          const field = columnFactory(data.title, data.dataIndex);
          if (data.isQuery) {
            field.queryConfig = {
              ...fieldFactory("input", data.title, data.dataIndex),
              isQuery: true,
            };
          }
          return field;
        });
      }
    },
  },
});
